package com.probiz.estoresf.customer.web.action;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.system.ModifyPasswordModel;
import com.probiz.estore.core.controller.GenericCustomerFrontController;
import com.probiz.estore.core.exception.ApplicationException;
import com.probiz.estore.customer.service.CustomerManager;
import com.probiz.estore.webapp.util.RequestContext;

public class ModifyPasswordFrontController extends GenericCustomerFrontController<ModifyPasswordModel> {
    private CustomerManager customerManager = null;
	@Override
	protected String getEntityName(ModifyPasswordModel entity) {
		return null;
	}

    public void setCustomerManager(CustomerManager customerManager) {
        this.customerManager = customerManager;
    }

	@Override
	protected void initController() throws Exception {
		
	}
	
	@Override
	protected Map<Integer, Map<String, Object>> getMultiSaveModel(	HttpServletRequest request) {
		return null;
	}
    
	@Override
    public ModelAndView defaultAction(HttpServletRequest request, HttpServletResponse response) {
		 ModelAndView mv = new ModelAndView("customer/csModifyPassword");
	     mv.addObject("modifyPasswordModel", new ModifyPasswordModel());
	     return mv;
    }
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.core.controller.GenericCustomerFrontController#preSave(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindException)
	 * 检查旧密码是否正确
	 */
	protected void preSave(HttpServletRequest request, ModifyPasswordModel passwordModel, BindException errors) {
		// check the old password is right
		boolean isOldPasswordRight = customerManager.getIsPasswordRight(passwordModel.getAppuserId(), passwordModel.getOldPassword());

		if (!isOldPasswordRight) {
			errors.rejectValue("oldPassword", "appUser.oldPassword.error", "Your old password is wrong.");
		}
	}
	
	@Override
    public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Customer customer = (Customer) RequestContext.getCurrentUser();
		ModifyPasswordModel passwordModel = new ModifyPasswordModel();
		passwordModel.setAppuserId(customer.getAppuserId());
		BindException errors = null;
		try {
			ServletRequestDataBinder binder = bindAndValidate(request, passwordModel);
			errors = new BindException(binder.getBindingResult());
			preSave(request, passwordModel, errors);
			if (!errors.hasErrors()) {
				
				// modify password
				customerManager.saveModifyPassword(passwordModel.getAppuserId(), passwordModel.getPassword());

				String message = getMessage("appUser.password.modified");
				saveMessage(request, message);
			}
		} catch (ApplicationException e) {
			handleApplicationException(errors, e);
		}
		
		ModelAndView mv = new ModelAndView("customer/csModifyPassword");
		if (errors.hasErrors()) {
			mv = getModelAndView("customer/csModifyPassword", errors.getModel());
		}else{
			mv.addObject("modifyPasswordModel", new ModifyPasswordModel());
		}
		return mv;
	}

	
	
}
